/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.sidebar.setting.item.update;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.launcher.sidebar.SettingData;
import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.UpgradeManager;
import com.coocaa.x.service.litecontent.api.ContentApi;
import com.coocaa.x.service.litecontent.data.system.com.coocaa.x.RP_Upgrade.Upgrade_Item;
import com.coocaa.x.service.liteupgrade.aidl.UpgradeParams;
import com.tianci.appstore.ui.about.AboutListener;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;

public class Update extends Item implements AboutListener {
    // private UpgradeDialog upgradeDlg = null;
    private SettingData mSettingData = null;

    public Update(Context context) {
        super(context);
        Upgrade_Item item = ContentApi.getUpdateItem();
        if (item != null && item.state == 2) {
            ((UpdateItem) mItemView).updateVersionInfo(item.verName);
        }
    }

    @Override
    public void onItemFocusChange(View v, boolean focus) {
        // TODO Auto-generated method stub
        super.onItemFocusChange(v, focus);
    }

    @Override
    public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(View v) {
        if (!LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable))
            return;
        CoocaaApplication.post(new Runnable() {
            @Override
            public void run() {
                UpgradeParams params = new UpgradeParams();
                params.params.put("device_channel", LiteAppApplication.getDeviceChannel());
                params.params.put("can_show_loading", Boolean.TRUE.toString());
                UpgradeManager.exec("enable_dialog", params);
                UpgradeManager.checkUpgrade(params);
            }
        });
        super.onItemClick(v);
    }

    @Override
    public ItemView createItemView() {
        return new UpdateItem(mContext);
    }

    @Override
    public void onRequestVersion(String version) {

    }

    @Override
    public void setData(Object object) {
        this.mSettingData = (SettingData) object;
        ((UpdateItem) mItemView).updateIcon(SkyConstants.getIconId(mSettingData.iconId));
        ((UpdateItem) mItemView).updateName(SkyConstants.getStringId(mSettingData.titleId));
    }
}
